//
//  PropertySet.hpp
//  RoutingEvaluator
//
//  Created by Christopher Kemsley on 4/15/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#include <string>
#include <map>
#include <sstream>
#include "Types.hpp"

NamespaceBegin





struct PropertySet
{
	// Creates from a map
	PropertySet ( std::map<std::string,std::string> const & allInfo )
	:	mInfo ( allInfo ) { }
	
	
	// Creates from the argc/argv as found in main
	// "exePath" is the app path
	PropertySet ( char const* *argv , int argc )
	:	mInfo ()
		{
		char const*		key		=	"exePath" ;
		while ( argc-- )
			{
			if ( key != 0 )
				{
				mInfo[key] = std::string(*(argv++)) ;
				key = 0 ;
				}
			else
				{
				key = *(argv++) ;
				}
			}
		}
	
	
	// Creates a copy
	PropertySet ( PropertySet const & copy )
	:	mInfo ( copy.mInfo ) { }
	
	
	// Creates empty
	PropertySet (  )
	:	mInfo () { }
	
	
	~PropertySet () { }
	
	
	void	setValueIfNotSet ( std::string const & inName , std::string const & inValue )
		{
		std::map<std::string,std::string>::const_iterator found = mInfo.find(inName) ;
		if ( found != mInfo.end() ) return ;
		
		mInfo[inName] = inValue ;
		}
	
	
	
	
	
	// Returns true if the value exists. If false is returned, outValue
	// will not be altered.
	template <typename Type>
	bool	getValue	( std::string const & inName , Type & outValue ) const
		{
		std::map<std::string,std::string>::const_iterator found = mInfo.find(inName) ;
		if ( found == mInfo.end() ) return false ;
		
		std::stringstream s ( found->second ) ;
		s >> outValue ;
		return true ;
		}
	
	bool	getValue	( std::string const & inName , std::string & outValue ) const
		{
		std::map<std::string,std::string>::const_iterator found = mInfo.find(inName) ;
		if ( found == mInfo.end() ) return false ;
		
		outValue = found->second ;
		return true ;
		}
	
	
	
	// Returns true if the value exists. If false is returned, outValue
	// will not be altered.
	template <typename Type>
	void	setValue	( std::string const & inName , Type const & inValue )
		{
		std::stringstream s ;
		s << inValue ;
		mInfo [inName] = s.str() ;
		}
	
	
private:
	std::map<std::string,std::string> mInfo ;
} ;


NamespaceEnd
